package com.yowasa.xiuxian.engine.message.hone;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.MysComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.ComponentUtil;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

@CMD(name = "/学习心法", scope = CmdScopeEnum.ALL)
public class StudyMentalHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);
        if (StringUtils.isNotBlank(user.getUserConfig().getMental()) && !user.getUserConfig().getMental().equals("太玄经") && !user.getUserConfig().getMental().equals("九转轮回功")) {
            throw new HandlerException("你已经学习了一种心法，不可重复学习");
        }
        List<String> canStudyList = Lists.newArrayList("长生六道诀", "炽凰百炼", "七杀剑诀", "托体同山诀", "玉清仙法");
        String param = getParam(message);
        if (StringUtils.isEmpty(param)) {
            List<List<MysComponent>> componentList = ComponentUtil.getComponentList("/学习心法 ",1,canStudyList);
            messageHelper.replyMysPanel(message, "可学习的心法有:",null,null,componentList);
            return;
        }
        if (!canStudyList.contains(param)) {
            throw new HandlerException(MessageFormat.format("未找到名为【{0}】的心法", param));
        }
        MentalInterface mental = HoneEngine.mentalMap.get(param);
        mental.limit(user);
        int days = mental.studyTime(user);
        user.getUserConfig().setMental(param);
        Date to = DateXUtil.getTo(days);
        user.setLockEndTime(to);
        user.setLockReason(MessageFormat.format(user.getName() + "正在学习{0}心法[{1}]", param, DateXUtil.formatTime(to)));
        user.getUserConfig().getCanStudyStrategies().addAll(mental.bsList());
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, MessageFormat.format("你消耗了{0}天学习{1}成功，心法介绍：{2}", days, param, mental.desc()));
    }

    public String desc(){
        return new StringBuffer().append("新手最重要的一个指令之一\n")
                .append("使用方式为 /学习心法 心法名称，不带名称会展示所有当前可学习的神通\n")
                .append("个人信息中会展示每个人激活的灵根，学习心法需要有对应的灵根\n心法无需获取，可直接学习，每本心法会有自带的战技与神通")
                .toString();

    }

}
